package com.chenqq.qsocket.tools;

import org.apache.mina.core.session.IoSession;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class IoSessionManager {
    private IoSessionManager() {
    }

    private static IoSessionManager inStance;
    private HashMap<String, HashMap<String, IoSession>> ioSessionHashMap = new HashMap<>();

    public  static IoSessionManager getInStance() {
        if (inStance == null) {
            inStance = new IoSessionManager();
        }
        return inStance;
    }

    public void addIoSession(String Tag, String address, IoSession ioSession) {
        HashMap<String, IoSession> stringIoSessionHashMap = ioSessionHashMap.get(Tag);
        if (stringIoSessionHashMap == null) {
            stringIoSessionHashMap = new HashMap<>();
        }
        stringIoSessionHashMap.put(address, ioSession);
        ioSessionHashMap.put(Tag, stringIoSessionHashMap);
    }

    public void removeIoSession(String Tag, String address) {
        HashMap<String, IoSession> stringIoSessionHashMap = ioSessionHashMap.get(Tag);
        if (stringIoSessionHashMap != null) {
            IoSession ioSession = stringIoSessionHashMap.get(address);
            if (ioSession != null) {
                ioSession.closeNow();
                stringIoSessionHashMap.remove(ioSession);
            }
            if (stringIoSessionHashMap.size() == 0) {
                ioSessionHashMap.remove(Tag);
            }
        }
    }

    public void removeIoSession(String Tag) {
        HashMap<String, IoSession> stringIoSessionHashMap = ioSessionHashMap.get(Tag);
        if (stringIoSessionHashMap != null) {
            Set<String> strings = stringIoSessionHashMap.keySet();
            for (String key : strings) {
                IoSession ioSession = stringIoSessionHashMap.get(key);
                if (ioSession != null)
                    ioSession.closeNow();
            }
        }
        ioSessionHashMap.remove(Tag);
    }


    public void writeMessage(String Tag, Object message) {
        HashMap<String, IoSession> stringIoSessionHashMap = ioSessionHashMap.get(Tag);
        if (stringIoSessionHashMap != null) {
            Set<String> strings = stringIoSessionHashMap.keySet();
            for (String key : strings) {
                IoSession ioSession = stringIoSessionHashMap.get(key);
                if (ioSession != null)
                    ioSession.write(message);
            }
        }
    }


    public void writeMessage(String Tag,String address,Object message){
        HashMap<String, IoSession> stringIoSessionHashMap = ioSessionHashMap.get(Tag);
        if (stringIoSessionHashMap != null) {
            IoSession ioSession = stringIoSessionHashMap.get(address);
            if (ioSession!=null){
                ioSession.write(message);
            }
        }
    }

    public void clearIoSession(){
        Set<String> strings = ioSessionHashMap.keySet();
        for (String key:strings){
            HashMap<String, IoSession> stringIoSessionHashMap = ioSessionHashMap.get(key);
            if (stringIoSessionHashMap!=null){
                Iterator<Map.Entry<String, IoSession>> iterator = stringIoSessionHashMap.entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, IoSession> next = iterator.next();
                    next.getValue().closeNow();
                }
                stringIoSessionHashMap.clear();
            }
        }
        ioSessionHashMap.clear();
    }

}
